Welcome to Django!

1.7 响应响应格式和状态码

Django默认响应格式是text/html,同时也给我们提供了修改的方式

def hello(request):

filepath=os.path.join(settings.BASE_DIR, "shn","hello_world.html")

with open (filepath,"r") as f:

content=f.read()

return HttpResponse(content, content_type = "text/html", status = 200 )

#content_type="text/html" 响应的格式是html格式,也可以改为纯文件格式cotent-type

#status=200 默认响应码是200,也可以修改成其他的3位数字

在网页中:

Headers/标头:

General/常规:

请求URL: http://127.0.0.1:8000/hello

请求方法: GET

状态代码: 127.0.0.1:8000

引用者策略: strict-origin-when-cross-origin

ResponseHeaders/响应标头(请求头):

Content-Length: 1324

Content-Type: text/html

Date: Sun, 28 Apr 2024 14:24:43 GMT

Server: WSGIServer/0.2 CPython/3.9.13

X-Content-Type-Options: nosniff

X-Frame-Options: DENY

请求标头:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9, image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

Accept-Encoding: gzip, deflate

Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

Cache-Control: max-age=0

Connection: keep-alive

Cookie:

JSESSIONID=1B8699C4522B9CF5108CD86B4F6CBFF0; LtpaToken=AAECAzY2MkU1RTg4NjYyRjA3NDhya3dlaTAxOTI2+MYqfeDHkvonXb4w0KiXQsTzRUY=; SESSION=M2FjNDRjZTEtZmQxYi00M2IzLTkxZjUtMWU5MmFhNTVhZGEw

Host: oa.shinning.com.cn:6932

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0